package com.example.springbootjpasqllogdemo.common.config;

import com.example.springbootjpasqllogdemo.common.exception.BusinessException;
import com.example.springbootjpasqllogdemo.common.result.ResultEnum;
import com.example.springbootjpasqllogdemo.common.util.PubUtils;
import org.modelmapper.AbstractConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.time.Instant;

/**
 * 实例化Long对象转换处理类
 *
 * @author Lv
 * @date 2022/11/4
 */
@Component
public class InstantLongConverter extends AbstractConverter<Instant, Long> implements Converter<Instant, Long> {
    /**
     * 转换方法
     *
     * @param instance 对象实例
     * @return
     */
    @Override
    public Long convert(Instant instance) {
        if (PubUtils.isNull(instance)) {
            return null;
        }
        try {
            return instance.toEpochMilli();
        } catch (Exception e) {
            throw new BusinessException(ResultEnum.INSTANT_LONG_CONVERTER_ERROR);
        }
    }
}
